﻿using System;
using System.Net.Sockets;

namespace Up2Link.Core
{
    /// <summary>
    /// 被类CServer和CClient使用
    /// 封装了类CTransBytes，通过调用CTransBytes的公共接口实现数据的接收与发送
    /// </summary>
    public class CTcpConnection
    {
        /// <summary>
        /// Tcp连接的套接字
        /// </summary>
        private TcpClient client = null;

        /// <summary>
        /// CTransBytes对象，用于发送接收字节消息
        /// </summary>
        private CTransBytes tb = null;

        /// <summary>
        /// Tcp连接状态
        /// </summary>
        private Int32 status = 0;

        /// <summary>
        /// 标记Tcp连接是否打开
        /// </summary>
        private Boolean clientOn = false;

        /// <summary>
        /// 默认无参数构造函数
        /// 用于创建泛型类对象
        /// </summary>
        public CTcpConnection()
        {
        }

        /// <summary>
        /// 构造函数
        /// 作为参数的Tcp套接字对象由CServer或CClient类创建，然后传递给构造函数
        /// </summary>
        /// <param name="client">TcpClient对象</param>
        public CTcpConnection(TcpClient client)
        {
            try
            {
                //从Tcp套接字获取网络流对象后，传递给CTransBytes的构造函数
                this.client = client;
                tb = new CTransBytes(client.GetStream());
                clientOn = true;
            }
            catch (Exception e)
            {
                //如果捕捉到异常，则关闭Tcp连接
                clientOn = false;
                //System.Console.WriteLine(e.StackTrace);
            }
        }

        /// <summary>
        /// 创建泛型类对象后使用这个函数初始化
        /// </summary>
        /// <param name="client"></param>
        //public void Init(TcpClient client)
        //{
        //    try
        //    {
        //        //从Tcp套接字获取网络流对象后，传递给CTransBytes的构造函数
        //        this.client = client;
        //        tb = new CTransBytes(client.GetStream());
        //        clientOn = true;
        //    }
        //    catch (Exception e)
        //    {
        //        //如果捕捉到异常，则关闭Tcp连接
        //        clientOn = false;
        //        //System.Console.WriteLine(e.StackTrace);
        //    }
        //}

        /// <summary>
        /// 调用CTransBytes的update函数
        /// </summary>
        public void Update()
        {
            if (clientOn)
            {
                tb.Update();

                //如果函数调用后网络流关闭，则关闭Tcp连接
                if (!tb.IsStreamOn())
                {
                    clientOn = false;
                }
            }            
        }

        /// <summary>
        /// 返回Tcp连接的运行状态
        /// </summary>
        /// <returns>Tcp连接的运行状态</returns>
        public Boolean IsClientOn()
        {
            return clientOn;
        }

        /// <summary>
        /// 关闭Tcp连接
        /// </summary>
        public void Close()
        {
            clientOn = false;
            tb.Close();
            client.Close();
        }

        /// <summary>
        /// 调用CTransBytes的Receive函数
        /// </summary>
        /// <returns>消息的字节数组，如果队列为空则返回null</returns>
        public Byte[] Receive()
        {
            return tb.Receive();
        }

        /// <summary>
        /// 调用CTransBytes的Send函数
        /// </summary>
        /// <param name="data">待发送消息的字节数组</param>
        public void Send(Byte[] data)
        {
            tb.Send(data);
        }

        /// <summary>
        /// 连接状态属性公共接口
        /// </summary>
        public Int32 Status
        {
            get { return status; }
            set { status = value; }
        }
    }
}
